package com.code.splider.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.code.splider.common.util.EPanluType;
import com.code.splider.dao.PanluMapper;
import com.code.splider.dao.ScorePanluMapper;
import com.code.splider.model.Panlu;
import com.code.splider.model.ScorePanlu;
import com.code.splider.service.PanluService;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;

@Service
public class PanluServiceImpl implements PanluService{
	
	@Resource
	private PanluMapper panluMapper;
	@Resource
	private ScorePanluMapper scorePanluMapper;
	
	@Override
	public void save(String url){
		List<ScorePanlu>  scores =  this.scorePanluMapper.selectAll();
		if(scores!=null&&!scores.isEmpty()){
			this.panluMapper.deleteAll();
			List<Panlu>  list = null;
			for (ScorePanlu scorePanlu : scores) {
				String id =scorePanlu.getZhiboId();
				String tempUrl=url+id;
				if(id!=null){
					 list = getPage(tempUrl,Long.valueOf(id));	
				}
				if(list!=null&&!list.isEmpty()){
					this.panluMapper.insertList(list);	
				}
				
			}
		}
	}
	
	private static List<Panlu> getPage(String url,Long scoreId){
		List<Panlu> result = null;
		 WebClient webClient = null;
		try {
			result = new ArrayList<Panlu>();
			webClient = new WebClient(BrowserVersion.FIREFOX_45);
			webClient.getOptions().setJavaScriptEnabled(true);
			webClient.setAjaxController(new NicelyResynchronizingAjaxController());
			webClient.getOptions().setThrowExceptionOnScriptError(false);
			webClient.getOptions().setCssEnabled(false);
			webClient.getOptions().setActiveXNative(false);
			HtmlPage page = webClient.getPage(url);
			//全部赛事-全部
			Panlu lu = new Panlu();
			page.getHtmlElementById("tabtypeid1").click();
			page.getHtmlElementById("tabid1").click();
			findPanlu(page,lu,EPanluType.AllAll,scoreId);
			result.add(lu);
			//全部赛事-主客
			lu = new Panlu();
			page.getHtmlElementById("tabtypeid1").click();
			page.getHtmlElementById("tabid2").click();
			findPanlu(page,lu,EPanluType.AllHome,scoreId);
			result.add(lu);
			//本赛事-全部
			lu = new Panlu();
			page.getHtmlElementById("tabtypeid2").click();
			page.getHtmlElementById("tabid1").click();
			findPanlu(page,lu,EPanluType.ThisAll,scoreId);
			result.add(lu);
			//本赛事-主客
			lu = new Panlu();
			page.getHtmlElementById("tabtypeid2").click();
			page.getHtmlElementById("tabid2").click();
			findPanlu(page,lu,EPanluType.TisHome,scoreId);
			result.add(lu);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			webClient.close();
		}
		return result;	
	}
	
	private static void findPanlu(HtmlPage page,Panlu lu,EPanluType type,Long scoreId) throws Exception{
		page.getHtmlElementById("td10").click();
		Thread.sleep(1500);
		HtmlDivision div = (HtmlDivision) page.getElementById("history_table");
		@SuppressWarnings({ "unchecked", "rawtypes" })
		DomNodeList<HtmlTable> nodes = (DomNodeList) div.getChildNodes();
		HtmlTable htmlTable = nodes.get(0);
		List<HtmlTableRow> list = htmlTable.getBodies().get(0).getRows();
		if(list.size()>=0){
		List<HtmlTableCell> cells0 = list.get(0).getCells();
		homeData(cells0, lu);
		}
		if(list.size()>2){
			List<HtmlTableCell> cells3 = list.get(2).getCells();
			awayData(cells3, lu);	
		}
		lu.setCreateTime(new Date());
		lu.setType(type.getKey());
		lu.setTypeDes(type.getValue());
		lu.setScoreId(scoreId);
	}
	
	private static void homeData(List<HtmlTableCell> cells,Panlu lu){
		String[] strs = null;
		if (cells != null && !cells.isEmpty()) {
			if (isCell(cells, 0)) {
				lu.setHomeTeam(teamSplit(cells.get(0).asText()));
			}
			if (isCell(cells, 2)) {
				strs = cellSplit(cells.get(2).asText());
				lu.setHomeHalfGoalin(strs[0]);
				lu.setHomeAllGoalin(strs[1]);
			}
			if (isCell(cells, 3)) {
				strs = cellSplit(cells.get(3).asText());
				lu.setHomeHalfFumble(strs[0]);
				lu.setHomeAllFumble(strs[1]);
			}
			if (isCell(cells, 4)) {
				strs = cellSplit(cells.get(4).asText());
				lu.setHomeHalfBigball(strs[0]);
				lu.setHomeAllBigball(strs[1]);
			}
			if (isCell(cells, 5)) {
				strs = cellSplit(cells.get(5).asText());
				lu.setHomeHalfLetball(strs[0]);
				lu.setHomeAllLetball(strs[1]);
			}
			if (isCell(cells, 6)) {
				strs = cellSplit(cells.get(6).asText());
				lu.setHomeHalfWin(strs[0]);
				lu.setHomeAllWin(strs[1]);
			}
		}
	}
	
	private static void awayData(List<HtmlTableCell> cells,Panlu lu){
		String[] strs = null;
		if (cells != null && !cells.isEmpty()) {
			if (isCell(cells, 0)) {
				lu.setAwayTeam(teamSplit(cells.get(0).asText()));
			}
			if (isCell(cells, 2)) {
				strs = cellSplit(cells.get(2).asText());
				lu.setAwayHalfGoalin(strs[0]);
				lu.setAwayAllGoalin(strs[1]);
			}
			if (isCell(cells, 3)) {
				strs = cellSplit(cells.get(3).asText());
				lu.setAwayHalfFumble(strs[0]);
				lu.setAwayAllFumble(strs[1]);
			}
			if (isCell(cells, 4)) {
				strs = cellSplit(cells.get(4).asText());
				lu.setAwayHalfBigball(strs[0]);
				lu.setAwayAllBigball(strs[1]);
			}
			if (isCell(cells, 5)) {
				strs = cellSplit(cells.get(5).asText());
				lu.setAwayHalfLetball(strs[0]);
				lu.setAwayAllLetball(strs[1]);
			}
			if (isCell(cells, 6)) {
				strs = cellSplit(cells.get(6).asText());
				lu.setAwayHalfWin(strs[0]);
				lu.setAwayAllWin(strs[1]);
			}
		}
	}
	
	public static boolean isCell(List<HtmlTableCell> cells,int length){
		if(cells!=null&&cells.size()>=length){
			return true;
		}
		return false;
	}
	
	public static String[] cellSplit(String str){
		String[] strs =  null;
		if(str!=null){
			strs = str.split("/");
			if(strs==null){
				strs = new String[]{"",""};
			}else if(strs.length==1){
				strs = new String[]{strs[0],""};
			}
		}else{
			strs = new String[]{"",""};
		}
		return strs;
	}
	
	public static String teamSplit(String str){
		String strs =  null;
		if(str!=null){
			strs = str.split("\r")[0];
		}
		return strs;
	}
	
	public static void main(String[] args) {
		String url= "http://www.dszuqiu.com/panlu/242486";
		List<Panlu>  list =	getPage(url,242486L);
		for (Panlu panlu : list) {
			System.out.println(panlu);
		}
	}
}
